<?php
function dmodel_new_class($query,$data=array()) {
	$a = dmodel_get_args($query,"name");	
	$p = dmodel_get_prefix($a);
	
	$d = new stdClass();
	$d->form = dmodel_get_class_form($a->form,$data);
	$d->name = $a->name;
	$d->mood = 1;
	
	if (!dmodel_hit_class($query,$data)) {
		if (!dmodel_hit_class($query)) {
			# class not exist by name create it
			$i = dmodel_sql_insert($d,"class");
			$q = "INSERT INTO {$p}class({$i->fields}) VALUES ({$i->values})";
			if ($a->get == "query") {return $q;}
			dmodel_query($q);		
		} else {
			# class exist by name update it definition
			$q = "UPDATE {$p}class SET form='{$d->form}' WHERE name='{$d->name}'";
			if ($a->get == "query") {return $q;}
			dmodel_query($q);
		}
	}
}

function dmodel_hit_class($query="",$data=array()) {
	$a = dmodel_get_args($query,"name");
	$p = dmodel_get_prefix($a);
	
	$d = new stdClass();
	$d->form = dmodel_get_class_form($a->form,$data);
	$d->name = $a->name;
	
	if ($d->form) {
		$q = "SELECT id FROM {$p}class WHERE name='{$d->name}' AND form='{$d->form}' LIMIT 1";
	} else {
		$q = "SELECT id FROM {$p}class WHERE name='{$d->name}' LIMIT 1";
	}
	
	if ($a->get == "query") {
		return $q;
	} else {
		$r = dmodel_query($q);	
		return $r;
	}
}

function dmodel_get_class($query="",$data=null) {
	$a = dmodel_get_args($query,"name");
	$p = dmodel_get_prefix();
	
	if ($a->name!="") {
		$q = "SELECT * FROM {$p}class WHERE name='{$a->name}' LIMIT 1";
	} else {
		$q = "SELECT * FROM {$p}class";
	}
	
	if ($a->get == "query") {
		return $q;
	} else {
		$r = dmodel_query($q);
		#return first 
		$r = $r[0];
		$r->form = dmodel_parse_class_form($r->form);
		return $r;
	}
}

function dmodel_parse_class_form($form) {
	$o = array();
	$temp = explode(";",$form);
	foreach($temp as $line) {
		$line = explode(":",$line);
		$line[0] = explode(" as ",$line[0]);
		$o[] = array(
			"field" => isset($line[0][1]) ? $line[0][1] : "none",
			"class" => $line[0][0],
			"space" => isset($line[1]) ? $line[1] : "none",
		);
	}
	return $o;
}

function dmodel_get_class_form($form,$data) {
	$line = array();
	foreach($data as $a=>$b) {
		if (is_string($a)) {
			$line[] = $a.':'.$b;
		} else {
			$line[] = $b.':';
		}
	}
	$form = ($form ? $form.';' : '').implode(";",$line);
	return $form;
}

	class the_model_class {
		function get_class($query,$data=null) {return oh_model_get_class($query,$data);}
		function old_get_class($query = "") {
			if (!$this->test()) return oh_this();					

			$a = $this->get_args($query,"name");
			$p = $this->get_prefix();
			$w = $this->get_where($a);
			$q = "SELECT * FROM {$p}class $w";
			$r = oh($this->datasource)->get_results($q);
			$c = count($r);

			if ($c > 0) {
				foreach($r as &$i) {					
					$i->fields = $this->parse_model_form($i->form);
				}
				return $r[0];
			}									
		}
		
		function set_class($query = "") {
		}
		function new_class($query="",$data="") {return oh_model_new_class($query,$data);}
		function old_new_class($query = "") {
			global $oh_object_model;
			if (!$this->test()) return oh_this();

			$args = $this->get_args($query,"name","define:form");			
			$args->form = $this->sanitize_model_form($args->form);			

			$prefix = $this->get_prefix();
			$i 		= $this->get_class_insert($args);
			$fields = $i["fields"];
			$values = $i["values"];
			if (!$this->hit_class($args)) {
				$q = "INSERT INTO {$prefix}class($fields) VALUES ($values)";
				oh($this->datasource)->query($q);					
			}		
			return $oh_object_model;
		}
		
		function hit_class($query,$data=null) {return oh_model_hit_class($query,$data);}
		
		function old_hit_class($query = "") {
			if ($this->test_installation()) {
				$args 	= $this->get_args($query,"name");
				$prefix = $this->get_prefix();				
				$w 		= $this->get_where($args);
				$q = "SELECT id FROM {$prefix}class $w LIMIT 1";
				$c = oh($this->datasource)->get_results($q);
				return count($c) > 0;
			}
		}
		
	
	
			
		function sanitize_model_form($formcode) {
			$formcode = preg_replace("/[ \r\t\n]/","",$formcode);
			$formcode = explode(",",$formcode);
			$formcode_output = array(); 
			foreach($formcode as $linecode) {
				$linecode = explode(":",$linecode);
				$type = trim($linecode[0]);
				$name = trim($linecode[1]);
				if ($name) {
					$op	= $type{0};
					if (($op!="!")||($op!="^")||($op!="_")) {
						if (false) {
						} else if (in_array($type,$this->model_object_fields["real"])) {
							$op = "!";
						} else if (in_array($type,$this->model_object_fields["meta"])) {
							$op = "_";						
						} else {
							$op = "^";						
						}
						
					} else {
						$op = "";
					}
					$mu = $name{0};
					if (($mu!="*")||($mu!="*")) {
						$mu = ".";
					} else {
						$mu = "*";
					}
					$formcode_output[] = $op.$type.':'.$mu.$name;
				}
			}
			$formcode_output = implode(",",$formcode_output);
			return $formcode_output;		
		}
		function parse_model_form($formcode) {
			$o = array();
			$o['real'] = array();
			$o['meta'] = array();
			$o['chil'] = array();
			$o['tyof'] = array();
						
			$formcode = explode(",",$formcode);
			foreach($formcode as $linecode) {
				$linecode = explode(":",$linecode);
				$type = $linecode[0];
				$name = $linecode[1];
				$op   = $type{0};
				$mu   = $name{0};
				$type = substr($type,1);
				$name = substr($name,1);
				if (false) {
				} else if ($op=="!") {
					$o['real'][$type] = $name;
				} else if ($op=="_") {
					$o['meta'][$type] = $name;
				} else if ($op=="^") {
					$o['chil'][$type] = $name;
				}
				$o['tyof'][$name] = $type;
			}
			return $o;
		}
	
	}

